package com.leejxx.health.controller;

import com.leejxx.health.common.MessageConst;
import com.leejxx.health.entity.PageResult;
import com.leejxx.health.entity.QueryPageBean;
import com.leejxx.health.entity.Result;
import com.leejxx.health.pojo.Role;
import com.leejxx.health.service.UserService;
import com.leejxx.health.vo.AddOrEditRoleParam;
import com.leejxx.health.vo.AddOrEditUserParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.core.userdetails.User;

import java.util.LinkedHashSet;
import java.util.List;

/**
 * @Author LeeJx
 * @ClassName UserController
 * @Description TODO
 * @date 2020/12/29 16:42
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @DubboReference
    private UserService userService;

//    @RequestMapping("/login")
//    public Result login(@RequestBody LoginParam loginParam, HttpServletRequest request) {
//        log.info("[用户-登录]data:{}", loginParam.getUsername());
//        User user = userService.findByUsername(loginParam.getUsername());
//        if (null == user || !user.getPassword().equals(loginParam.getPassword())) {
//            return new Result(false, MessageConst.LOGIN_FAIL);
//        } else {
//            request.getSession().setAttribute("loginUser", user);
//            return new Result(true, MessageConst.LOGIN_SUCCESS, user);
//        }
//    }

    @RequestMapping("/loginSuccess")
    public Result loginSuccess() {
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }

    @RequestMapping("/loginFail")
    public Result loginFail() {
        return new Result(false, "登录失败");
    }

    @RequestMapping("/getUsername")
    public Result getUsername() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (null != principal && principal instanceof User) {
            User user = (User) principal;
            return new Result(true, MessageConst.GET_USERNAME_SUCCESS, user.getUsername());
        }
        return new Result(false, MessageConst.GET_USERNAME_FAIL);
    }

    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("[用户管理-分页查询]data:{}", queryPageBean);
        PageResult pageResult = userService.findByPage(queryPageBean);
        log.info("[用户管理-分页查询成功]data:{}", pageResult);
        return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
    }

    @RequestMapping("/findAllRoles")
    public Result findAllRoles() {
        LinkedHashSet<Role> roles = userService.findAllRoles();
        log.info("[用户管理-新增用户用户回显成功]data:{}", roles);
        return new Result(true, MessageConst.ACTION_SUCCESS, roles);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody AddOrEditUserParam userParam) {
        log.info("[用户管理-新增用户]data:{}", userParam);
        userService.addUser(userParam.getUser(), userParam.getRoleIds());
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @RequestMapping("/findUserById")
    public Result findUserById(Integer userId) {
        log.info("[用户管理-修改回显]data:{}", userId);
        com.leejxx.health.pojo.User user = userService.findUserById(userId);
        log.info("[用户管理-修改回显成功]data:{}", user);
        return new Result(true, MessageConst.ACTION_SUCCESS, user);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody AddOrEditUserParam userParam) {
        log.info("[用户管理-修改用户]data:{}", userParam);
        userService.edit(userParam.getUser(), userParam.getRoleIds());
        log.info("[用户管理-修改用户成功]");
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer userId) {
        log.info("[用户管理-删除用户]data:{}", userId);
        userService.deleteById(userId);
        log.info("[用户管理-删除用户成功]");
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @RequestMapping("/findUsername")
    public Result findUsername(Integer userId) {
        log.info("[用户管理-用户名回显]data:{}", userId);
        String username = userService.findUsernameByUserId(userId);
        log.info("[用户管理-用户名回显成功]data:{}", username);
        com.leejxx.health.pojo.User user = new com.leejxx.health.pojo.User();
        user.setUsername(username);
        return new Result(true, MessageConst.ACTION_SUCCESS, user);
    }

    @RequestMapping("/editPassword")
    public Result editPassword(@RequestBody AddOrEditUserParam userParam) {
        log.info("[用户管理-修改密码]data:{}", userParam);
        userService.editPassword(userParam.getUser());
        log.info("[用户管理-修改密码成功]");
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

}
